import {
	ref,
	nextTick
} from "vue";
export default function useTable() { // isList 是否是瀑布流处理
	const page = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const loading = ref(false)
	const isLastPage = ref(false)
	const list = ref([])

	const reset = () => {
		return new Promise((resolve) => {
			list.value = []
			isLastPage.value = false
			page.value = 1
			loading.value = false
			resolve()
		})
	}

	const result = (data) => {
		total.value = data.total
		loading.value = false
	}

	const complete = async (res) => {
		if (isLastPage.value) return
		loading.value = false
		page.value++
		total.value = res.total
		isLastPage.value = res.current == res.pages
		// 正常列表处理
		if (page.value === 1) {
			list.value = res.records

		} else {
			list.value = list.value.concat(res.records)
		}
	}

	return {
		page,
		pageSize,
		list,
		total,
		loading,
		isLastPage,
		reset,
		result,
		complete,
	}
}